package com.android.mios.model;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;

public class Room {
	
	public int serialNumber;
	public int id;
	public String name;
	public int section;
	
	public static class RoomComparator implements Comparator<Room> {
		public int compare(Room object1, Room object2) {
			if (object1.name == null && object2.name == null) {
				return 0;
			} else if(object1.name == null) {
				return -1;
			} else if (object2.name == null) {
				return 1;
			} else {
				return object1.name.compareToIgnoreCase(object2.name);
			}
		}
	}
	
	public static void sortRooms(Room[] rooms) {
		Arrays.sort(rooms, new RoomComparator());
	}
	
	/**
	 * Sort rooms and return a list of room in sorted list including a zero-room at the beginning of the list.
	 */
	public static int[] getSortedRooms(Map<Integer, Room> roomMap) {
		Room[] rooms = roomMap.values().toArray(new Room[roomMap.size()]);
		sortRooms(rooms);
		int[] roomIDs = new int[rooms.length + 1];
		int i = 0;
		roomIDs[0] = i++;
		for (Room room: rooms) {
			roomIDs[i++] = room.id;
		}
		return roomIDs;
	}

	@Override
	public String toString() {
		return name;
	}
}
